package com.suirich.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.suirich.bean.ProductCategories;
import com.suirich.dao.ProductCategoriesDao;

@Repository
public class ProductCategoriesDaoImpl extends GenericDaoHibernateImpl<ProductCategories, Long> implements ProductCategoriesDao {
	public ProductCategoriesDaoImpl() {
		super(ProductCategories.class);
	}

	@SuppressWarnings("unchecked")
	public List<ProductCategories> getProductCategoriesList(Long parentId) {
		String hql = "from ProductCategories p where p.parentId " +
				(parentId==-1?"!=":"=") + " :parentId order by p.updateDate desc,p.createDate desc";
		
		Query query = this.getCurrentSession().createQuery(hql);
		query.setLong("parentId", parentId==-1?0:parentId);
		
		return (List<ProductCategories>)query.list();
	}

    public void deleteProductCategoriesByParentId(Long parentId) {
        String hql = "delete from ProductCategories p where p.parentId = :parentId";
        
        Query query = this.getCurrentSession().createQuery(hql);
        query.setLong("parentId", parentId);
        
        query.executeUpdate();
    }
}
